package com.weixing.mall.provider.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AgentInfo;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.provider.api.model.vo.RoleVo;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.model.query.AgentBonusQuery;
import com.weixing.mall.provider.model.vo.AgentBonusVo;
import com.weixing.mall.provider.model.vo.AgentWithdrawBonusVo;
import com.weixing.mall.provider.service.IAgentBonusService;
import com.weixing.mall.provider.support.excel.BonusExcelDownData;
import com.weixing.mall.provider.util.AgentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 代理商分红接口
 *
 * @author  siashan
 * @since  v1.0
 */
@RestController
@RequestMapping("agent/bonus")
public class AgentBonusController extends BaseController {
    @Autowired
    private IAgentBonusService agentBonusService;
    @Autowired
    private SysUserRpcService sysUserRpcService;

    /**
     * 代理商分红列表--管理员用
     *
     * @param query    查询条件
     * @return         分红列表
     */
    @GetMapping("")
    public Result<IPage<AgentBonusVo>> List(AgentBonusQuery query) {
        Long uid = AuthUtil.getSysExt().getUid();
        IPage<AgentBonusVo> pageInfo = null;
        List<RoleVo> list = sysUserRpcService.getRoleById(uid).getData();
        for (RoleVo vo : list) {
            if (vo.getRoleCode().equals(GlobalConstant.RoleCode.ADMIN)) {
                pageInfo = agentBonusService.selectListOfPage(initMpPage(), query);
                return ResultUtil.success(pageInfo);
            }
        }
        for (RoleVo vo : list) {
            if (vo.getRoleCode().equals(GlobalConstant.RoleCode.KFADMIN)) {
                pageInfo = agentBonusService.selectListOfPage(initMpPage(), query);
                return ResultUtil.success(pageInfo);
            }
        }
        query.setCustomer(uid);
        return ResultUtil.success(agentBonusService.selectListOfPage(initMpPage(), query));
    }

    /**
     * 代理分红列表:下载Excel
     * @param query     查询条件
     * @param response  HttpServletResponse
     */
    @GetMapping("excel")
    @SysLog(type = OperateEnum.D,desp = "代理分红列表导出")
    public void excel(AgentBonusQuery query, HttpServletResponse response) {
        List<AgentBonusVo> voList = agentBonusService.selectList(query);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        try {
            String fileName = URLEncoder.encode("代理商分红列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), BonusExcelDownData.class).sheet("sheet1").doWrite(AgentUtil.wapperBonusExcelData(voList));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 代理商提现分红展示列表
     *
     * @apiNote  这个接口有用吗？
     *
     * @param id   提现id
     * @return     提现记录
     */
    @PostMapping("agentWithdrawBonusList/{id}")
    public Result<IPage<AgentWithdrawBonusVo>> agentWithdrawBonusList(Long id) {
        return ResultUtil.success(agentBonusService.agentWithdrawBonusList(initMpPage(), id));
    }


    /**
     * 代理商分红列表--代理商用
     *
     * @param query   查询条件
     * @return        分红列表
     */
    @GetMapping("/agent")
    public Result<IPage<AgentBonusVo>> ListForAgent(@AgentInfo AgentAuth auth, AgentBonusQuery query) {
        query.setAgentId(auth.getAid());
        return ResultUtil.success(agentBonusService.selectListOfPage(initMpPage(), query));
    }
}
